package com.gc.server.controller.api;

import com.gc.server.entity.BannerEntity;
import com.gc.server.entity.NoticeEntity;
import com.gc.server.entity.UserEntity;
import com.gc.server.enums.BannerTypeEnum;
import com.gc.server.service.BannerService;
import com.gc.server.service.NoticeService;
import com.gc.server.service.UserService;
import com.github.pagehelper.PageHelper;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.tz.entity.MallGoodsEntity;
import com.yf.tz.entity.MallTypeEntity;
import com.yf.tz.service.MallGoodsService;
import com.yf.tz.service.MallSkuService;
import com.yf.tz.service.MallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/home")
public class ApiHomeController {

    @Autowired
    private BannerService bannerService;

    //h5首页幻灯片
    @GetMapping("/banner")
    public R banner(){
        List<BannerEntity> list = bannerService.findByType(BannerTypeEnum.H5.getIndex());
        return R.ok(list);
    }

    @Autowired
    private MallGoodsService goodsService;

    @Autowired
    private MallTypeService mallTypeService;

    /**
     * 小程序商城首页
     *
     * @return
     */
    @GetMapping("/mall")
    @IgnoreAuth
    public R mall() {
        //获取所有banner
        List<BannerEntity> banners = bannerService.findByType(BannerTypeEnum.WX.getIndex());
        //推荐到首页的产品
        List<MallGoodsEntity> goodsList = goodsService.findTop();
        //所有类型
        List<MallTypeEntity> types = mallTypeService.queryList(null);
        return R.ok().putExtra("banners", banners).putExtra("goodsList", goodsList).putExtra("types", types);
    }



    /**
     * 商城产品列表
     * 小程序
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/mall/goods")
    @IgnoreAuth
    private R mallGoods(int page, int size, Long type, String name) {
        PageHelper.startPage(page, size).setOrderBy("idx desc");
        List<MallGoodsEntity> goodsList = goodsService.findByTypeAndName(type, name);
        return R.ok(goodsList);
    }

    @Autowired
    private MallSkuService skuService;

    /**
     * 产品详情
     * 小程序
     * @param id
     * @return
     */
    @GetMapping("/mall/goods/{id}")
    private R goodsInfo(@PathVariable Long id) {
        MallGoodsEntity mallGoodsEntity = goodsService.get(id);
        mallGoodsEntity.setSkuList(skuService.findSkuByGoodsId(id));
        mallGoodsEntity.setSkuItemList(skuService.findSkuItemByGoodsId(id));
        return R.ok(mallGoodsEntity);
    }


    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserService userService;

    @GetMapping("/notice")
    public R notice(@LoginUserId Long userId){
        UserEntity userEntity = userService.get(userId);
        NoticeEntity noticeEntity = noticeService.findNew(userEntity.getLevelId());
        return R.ok(noticeEntity);
    }

}
